local TITAN_VOLUME_ID = "Volume";
local TITAN_VOLUME_FRAME_SHOW_TIME = 0.5;
local TITAN_VOLUME_ARTWORK_PATH = "Interface\\AddOns\\TitanVolume\\Artwork\\";

function TitanPanelVolumeButton_OnLoad(self)
	self.registry = { 
		id = TITAN_VOLUME_ID,
		builtIn = 1,
		version = TITAN_VERSION,
		menuText = TITAN_VOLUME_MENU_TEXT, 
		tooltipTitle = TITAN_VOLUME_TOOLTIP, 
		tooltipTextFunction = "TitanPanelVolumeButton_GetTooltipText",
		iconWidth = 32,
		iconButtonWidth = 18,
	};
	
	table.insert(TITAN_PANEL_NONMOVABLE_PLUGINS, TITAN_VOLUME_ID)
end

function TitanPanelVolumeButton_OnShow()
	TitanPanelVolume_SetVolumeIcon();
end

function TitanPanelVolumeButton_OnEnter()
	-- Confirm master volume value
	TitanPanelMasterVolumeControlSlider:SetValue(1 - GetCVar("Sound_MasterVolume"));
	TitanPanelAmbienceVolumeControlSlider:SetValue(1 - GetCVar("Sound_AmbienceVolume"));
	TitanPanelSoundVolumeControlSlider:SetValue(1 - GetCVar("Sound_SFXVolume"));
	TitanPanelMusicVolumeControlSlider:SetValue(1 - GetCVar("Sound_MusicVolume"));
	TitanPanelMicrophoneVolumeControlSlider:SetValue(1 - GetCVar("OutboundChatVolume"));
	TitanPanelSpeakerVolumeControlSlider:SetValue(1 - GetCVar("InboundChatVolume"));
	TitanPanelVolume_SetVolumeIcon();
	reset_Master = GetCVar("Sound_MasterVolume");
	reset_Ambience = GetCVar("Sound_AmbienceVolume");
	reset_Sound = GetCVar("Sound_SFXVolume");
	reset_Music = GetCVar("Sound_MusicVolume");
	reset_Microphone = GetCVar("OutboundChatVolume");
	reset_Speaker = GetCVar("InboundChatVolume");
	SetCVar("Sound_MasterVolume", 0);
	SetCVar("Sound_AmbienceVolume", 0);
	SetCVar("Sound_SFXVolume", 0);
	SetCVar("Sound_MusicVolume", 0);
	SetCVar("OutboundChatVolume", 0);
	SetCVar("InboundChatVolume", 0);
	SetCVar("Sound_MasterVolume", reset_Master);
	SetCVar("Sound_AmbienceVolume", reset_Ambience);
	SetCVar("Sound_SFXVolume", reset_Sound);
	SetCVar("Sound_MusicVolume", reset_Music);
	SetCVar("OutboundChatVolume", reset_Microphone);
	SetCVar("InboundChatVolume", reset_Speaker);
end

-- 'Master' 
function TitanPanelMasterVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("Sound_MasterVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelMasterVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelMasterVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("Sound_MasterVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(0, 1);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("Sound_MasterVolume"));
	
	local position = TitanUtils_GetRealPosition(TITAN_VOLUME_ID);
     
     TitanPanelVolumeControlFrame:SetPoint("BOTTOMRIGHT", "TitanPanel" .. TitanUtils_GetWhichBar(TITAN_VOLUME_ID) .."Button", "TOPRIGHT", 0, 0);
     if (position == TITAN_PANEL_PLACE_TOP) then 
          TitanPanelVolumeControlFrame:ClearAllPoints();
          TitanPanelVolumeControlFrame:SetPoint("TOPLEFT", "TitanPanel" .. TitanUtils_GetWhichBar(TITAN_VOLUME_ID) .."Button", "BOTTOMLEFT", UIParent:GetRight() - TitanPanelVolumeControlFrame:GetWidth(), -4);
     else
          TitanPanelVolumeControlFrame:ClearAllPoints();
          TitanPanelVolumeControlFrame:SetPoint("BOTTOMLEFT", "TitanPanel" .. TitanUtils_GetWhichBar(TITAN_VOLUME_ID) .."Button", "TOPLEFT", UIParent:GetRight() - TitanPanelVolumeControlFrame:GetWidth(), 0);
     end   
	
end

function TitanPanelMasterVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.01);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.01);
	end
	
	SetCVar("Sound_MasterVolume", 1 - self:GetValue());
	
	TitanPanelVolume_SetVolumeIcon();

	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end
-- 'Music'
function TitanPanelMusicVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("Sound_MusicVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelMusicVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelMusicVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("Sound_MusicVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(0, 1);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("Sound_MusicVolume"));
end

function TitanPanelMusicVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.01);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.01);
	end

	SetCVar("Sound_MusicVolume", 1 - self:GetValue());
	
	
	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end
-- 'Sound'
function TitanPanelSoundVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("Sound_SFXVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelSoundVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelSoundVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("Sound_SFXVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(0, 1);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("Sound_SFXVolume"));
end

function TitanPanelSoundVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.01);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.01);
	end
	
	SetCVar("Sound_SFXVolume", 1 - self:GetValue());
	
	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end
-- 'Ambience'
function TitanPanelAmbienceVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("Sound_AmbienceVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelAmbienceVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelAmbienceVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("Sound_AmbienceVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(0, 1);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("Sound_AmbienceVolume"));
end

function TitanPanelAmbienceVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.01);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.01);
	end
	
	SetCVar("Sound_AmbienceVolume", 1 - self:GetValue());
	
	
	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end

function TitanPanelVolume_GetVolumeText(volume)
	return tostring(floor(100 * volume + 0.5)) .. "%";
end

-- 'Microphone'
function TitanPanelMicrophoneVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("OutboundChatVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelMicrophoneVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelMicrophoneVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("OutboundChatVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(-1.50, 0.75);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("OutboundChatVolume"));
end

function TitanPanelMicrophoneVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.02);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.02);
	end
	
	SetCVar("OutboundChatVolume", 1 - self:GetValue());
	
	
	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end

function TitanPanelVolume_GetVolumeText(volume)
	return tostring(floor(100 * volume + 0.5)) .. "%";
end

-- 'Speaker'
function TitanPanelSpeakerVolumeControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(GetCVar("InboundChatVolume")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

function TitanPanelSpeakerVolumeControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_VOLUME_FRAME_SHOW_TIME);
end

function TitanPanelSpeakerVolumeControlSlider_OnShow(self)        
	getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(GetCVar("InboundChatVolume")));
	getglobal(self:GetName().."High"):SetText(TITAN_VOLUME_CONTROL_LOW);
	getglobal(self:GetName().."Low"):SetText(TITAN_VOLUME_CONTROL_HIGH);
	self:SetMinMaxValues(0, 1);
	self:SetValueStep(0.01);
	self:SetValue(1 - GetCVar("InboundChatVolume"));
end

function TitanPanelSpeakerVolumeControlSlider_OnValueChanged(self, a1)
getglobal(self:GetName().."Text"):SetText(TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
local tempval = self:GetValue();
	
	if a1 == -1 then
	  self:SetValue(tempval + 0.01);
	end
	
	if a1 == 1 then
	  self:SetValue(tempval - 0.01);
	end
	
	SetCVar("InboundChatVolume", 1 - self:GetValue());
	
	
	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(TITAN_VOLUME_CONTROL_TOOLTIP, TitanPanelVolume_GetVolumeText(1 - self:GetValue()));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end

function TitanPanelVolume_GetVolumeText(volume)
	return tostring(floor(100 * volume + 0.5)) .. "%";
end

function TitanPanelVolumeControlFrame_OnLoad(self)
	getglobal(self:GetName().."Title"):SetText(TITAN_VOLUME_CONTROL_TITLE);
	getglobal(self:GetName().."MasterTitle"):SetText(TITAN_VOLUME_MASTER_CONTROL_TITLE);
	getglobal(self:GetName().."MusicTitle"):SetText(TITAN_VOLUME_MUSIC_CONTROL_TITLE);
	getglobal(self:GetName().."SoundTitle"):SetText(TITAN_VOLUME_SOUND_CONTROL_TITLE);
	getglobal(self:GetName().."AmbienceTitle"):SetText(TITAN_VOLUME_AMBIENCE_CONTROL_TITLE);
	getglobal(self:GetName().."MicrophoneTitle"):SetText(TITAN_VOLUME_MICROPHONE_CONTROL_TITLE);
	getglobal(self:GetName().."SpeakerTitle"):SetText(TITAN_VOLUME_SPEAKER_CONTROL_TITLE);
	self:SetBackdropBorderColor(1, 1, 1);
	self:SetBackdropColor(0, 0, 0, 1);
end

function TitanPanelVolumeControlFrame_OnUpdate(self, elapsed)
	TitanUtils_CheckFrameCounting(self, elapsed);
end

function TitanPanelVolume_SetVolumeIcon()
	local icon = getglobal("TitanPanelVolumeButtonIcon");
	local masterVolume = tonumber(GetCVar("Sound_MasterVolume"));
	if (masterVolume <= 0) then
		icon:SetTexture(TITAN_VOLUME_ARTWORK_PATH.."TitanVolumeMute");
	elseif (masterVolume < 0.33) then
		icon:SetTexture(TITAN_VOLUME_ARTWORK_PATH.."TitanVolumeLow");
	elseif (masterVolume < 0.66) then
		icon:SetTexture(TITAN_VOLUME_ARTWORK_PATH.."TitanVolumeMedium");
	else
		icon:SetTexture(TITAN_VOLUME_ARTWORK_PATH.."TitanVolumeHigh");
	end	
end

function TitanPanelVolumeButton_GetTooltipText()
	local volumeMasterText = TitanPanelVolume_GetVolumeText(GetCVar("Sound_MasterVolume"));
	local volumeSoundText = TitanPanelVolume_GetVolumeText(GetCVar("Sound_SFXVolume"));
	local volumeMusicText = TitanPanelVolume_GetVolumeText(GetCVar("Sound_MusicVolume"));
	local volumeAmbienceText = TitanPanelVolume_GetVolumeText(GetCVar("Sound_AmbienceVolume"));
	local volumeMicrophoneText = TitanPanelVolume_GetVolumeText(GetCVar("OutboundChatVolume"));
	local volumeSpeakerText = TitanPanelVolume_GetVolumeText(GetCVar("InboundChatVolume"));
	return ""..
		TITAN_VOLUME_MASTER_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeMasterText).."\n"..
		TITAN_VOLUME_SOUND_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeSoundText).."\n"..
		TITAN_VOLUME_MUSIC_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeMusicText).."\n"..
		TITAN_VOLUME_AMBIENCE_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeAmbienceText).."\n"..
		TITAN_VOLUME_MICROPHONE_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeMicrophoneText).."\n"..
		TITAN_VOLUME_SPEAKER_TOOLTIP_VALUE.."\t"..TitanUtils_GetHighlightText(volumeSpeakerText).."\n"..
		TitanUtils_GetGreenText(TITAN_VOLUME_TOOLTIP_HINT1).."\n"..
		TitanUtils_GetGreenText(TITAN_VOLUME_TOOLTIP_HINT2);
end

function TitanPanelRightClickMenu_PrepareVolumeMenu()
	TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_VOLUME_ID].menuText);
	TitanPanelRightClickMenu_AddCommand(TITAN_PANEL_MENU_HIDE, TITAN_VOLUME_ID, TITAN_PANEL_MENU_FUNC_HIDE);
end
